package com.zelin;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/25 15:31
 */
public class MongoDBDemo {
    public static void main(String[] args) {
        //findAll();
        //findByWords();
        //findByWords2();
        inserOne();
    }
    //查询浏览记录大于1000的记录
    private static void findByWords2(){
        //1.定义连接远程服务端的客户端
        MongoClient client = new MongoClient("192.168.25.6");
        //2.得到mogodb的数据库对象
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //3.从数据库中得到集合（相当于关系型数据库的表）
        MongoCollection<Document> spitDocument = spitdb.getCollection("spit");
        BasicDBObject basicDBObject = new BasicDBObject("visits",new BasicDBObject("$gt",1000));
        //4.得到文档集合
        FindIterable<Document> documents = spitDocument.find(basicDBObject);
        //5.打印信息
        printInfo(documents);
        //6.关闭
        client.close();
    }
    //添加数据
    private static  void  inserOne(){
        //1.定义连接远程服务端的客户端
        MongoClient client = new MongoClient("192.168.25.6");
        //2.得到mogodb的数据库对象
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //3.从数据库中得到集合（相当于关系型数据库的表）
        MongoCollection<Document> spitDocument = spitdb.getCollection("spit");
        //4.定义要添加的对象（先放到map中）
        Map map = new HashMap();
        map.put("_id","11");
        map.put("content","欢迎学习java");
        map.put("userid","1012");
        map.put("visits","1000");
        map.put("nickname","小马");
        Document document = new Document(map);
        spitDocument.insertOne(document);
        //5.关闭
        client.close();
    }
    //根据条件进行查询(查询userid=1012的记录)
    private static void findByWords() {
        //1.定义连接远程服务端的客户端
        MongoClient client = new MongoClient("192.168.25.6");
        //2.得到mogodb的数据库对象
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //3.从数据库中得到集合（相当于关系型数据库的表）
        MongoCollection<Document> spitDocument = spitdb.getCollection("spit");
        BasicDBObject basicDBObject = new BasicDBObject("userid","1012");
        //4.得到文档集合
        FindIterable<Document> documents = spitDocument.find(basicDBObject);
        //5.打印信息
        printInfo(documents);
        //6.关闭
        client.close();
    }

    //查询所有
    private static void findAll() {
        //1.定义连接远程服务端的客户端
        MongoClient client = new MongoClient("192.168.25.6");
        //2.得到mogodb的数据库对象
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //3.从数据库中得到集合（相当于关系型数据库的表）
        MongoCollection<Document> spitDocument = spitdb.getCollection("spit");
        //4.得到文档集合
        FindIterable<Document> documents = spitDocument.find();
        //5.遍历集合打印每个document对象（一行记录）
        printInfo(documents);
        //6.关闭客户端
        client.close();
    }
    //打印信息
    private static void printInfo(FindIterable<Document> documents) {
        for (Document document : documents) {
            String content = document.getString("content");
            String userid = document.getString("userid");
            String nickname = document.getString("nickname");
            Object visits = document.get("visits");
            System.out.println("conent:" + content);
            System.out.println("userid:" + userid);
            System.out.println("nickname:" + nickname);
            System.out.println("visits:" + visits);
            System.out.println("--------------------------------------------");
        }
    }


}
